/**
 * 
 */
package com.alstudio.core.telephone;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.core.voip.ALVoipCallManager;
import com.alstudio.utils.android.AndroidUtils;
import com.alstudio.utils.log.ALLog;

/**
 * @author alonso lee
 * 
 */
public class ALTelephoneCallStateManager {

	private static ALTelephoneCallStateManager mInstance;
	private TelephonyManager mTelephoneManager;

	public static ALTelephoneCallStateManager getDefaultManager() {
		if (mInstance == null) {
			mInstance = new ALTelephoneCallStateManager();
		}
		return mInstance;
	}

	/**
	 * 
	 */
	public ALTelephoneCallStateManager() {
		// TODO Auto-generated constructor stub
		mTelephoneManager = AndroidUtils.getTelephonyManager(ALLocalEnv
				.getLocalEnv());
		mTelephoneManager.listen(mPhoneCallStateListener,
				PhoneStateListener.LISTEN_CALL_STATE);
	}

	private PhoneStateListener mPhoneCallStateListener = new PhoneStateListener() {
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			super.onCallStateChanged(state, incomingNumber);

			switch (state) {
			case TelephonyManager.CALL_STATE_RINGING:
				// 有来电
			case TelephonyManager.CALL_STATE_OFFHOOK:
				// 拨出电话
				ALVoipCallManager.getDefaultALVoipManager().forceFinishCall();
				break;
			case TelephonyManager.CALL_STATE_IDLE:
				break;
			}

		}
	};

}
